/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package rad.Java.MotelPlus.Class;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import rad.Java.Common.Class.DefaultClass;
import rad.Java.Common.Class.IClass;
import rad.Java.Common.DB.GenerateSql;

/**
 *
 * @author Renato ferreira dos santos
 */
public class Product extends DefaultClass implements IClass{

    // <editor-fold defaultstate="collapsed" desc="Declaration of variables">
        private String _code;
        private String _name;
        private String _price;
        private String _quant;
    // </editor-fold>

    public Product()
    {
        _gsql.setTable("product");
        _sequence = "product_product_id_seq";
    }

    // <editor-fold defaultstate="collapsed" desc="Declaration of methods Get and Set">
        public String getCode()
        {
            return _code;
        }

        public void setCode(String value)
        {
            this._code = value;
        }
        
        public String getName() {
            return _name;
        }

        public void setName(String _name) {
            this._name = _name;
        }

        public String getPrice() {
            return _price;
        }

        public void setPrice(String _price) {
            this._price = _price;
        }

        public String getQuant() {
            return _quant;
        }

        public void setQuant(String _quant) {
            this._quant = _quant;
        }
    // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="Declaration of functions">
        public void clear()
        {
            _id             = "";
            _code           = "";
            _name           = "";
            _price          = "";
            _quant          = "";
        }

        @Override
        public void get(ResultSet rs)
        {
            try {
                _id             = rs.getString("product_id");
                _code           = rs.getString("product_code");
                _name           = rs.getString("product_name");
                _price          = rs.getString("product_price");
                _quant          = rs.getString("product_quant");
            } catch (SQLException ex) {
                Logger.getLogger(Product.class.getName()).log(Level.SEVERE, null, ex);
            }
        }

        @Override
        public void setGsql()
        {
            _gsql.setValue( _id                 , "product_id");
            _gsql.setValue( _code               , "product_code");
            _gsql.setValue( _name               , "product_name");
            _gsql.setValue( _price              , "product_price");
            _gsql.setValue( _quant              , "product_quant");
        }

        public boolean getByCode(String value) throws SQLException
        {
            _gsql.prepare();
            _gsql.setValue("", "*");
            _gsql.setCommandType(GenerateSql.CommandType.CMD_SELECT);
            _gsql.setConditionalWhere("product_code=" + value);
            if (_gsql.execute(true))
            {
                if (_gsql.next())
                {
                    get(_gsql.getResultSet());
                    return true;
                }
            }
            return false;
        }

        public boolean deleteByCode(String value)
        {
            _gsql.prepare();
            _gsql.setCommandType(GenerateSql.CommandType.CMD_DELETE);
            _gsql.setConditionalWhere("product_code=" + value);

            if (_gsql.execute(false))
                    return true;
            _msg_erro= _gsql.getMsgErro();
            return false;
        }
    // </editor-fold>
}
